<?php

namespace api\modules\v1\controllers;

use api\controllers\OnAuthController;
use api\modules\v1\forms\base\DepartmentForm;
use common\helpers\CommonHelper;
use common\models\base\Department;
use services\base\DepartmentService;
use Yii;

/**
 * Class DepartmentController
 * @package api\modules\v1\controllers
 */
class DepartmentController extends OnAuthController
{
    public $modelClass = Department::class;


    /**
     * @api {get} /v1/department/list  部门列表
     * @apiName list
     * @apiGroup Department
     * @apiUse CommonParam
     *
     * @apiParam {Number} status            状态
     * @apiParam {String} order_column      排序字段【account_num部门人数】
     * @apiParam {String} order_sort        排序值【asc正序、desc倒序】
     * @apiParam {String} page              页码
     * @apiParam {String} pageSize          每页展示数
     *
     * @apiSuccess {Object}  data                       返回数据
     * @apiSuccess {Number}  data.page                  页码
     * @apiSuccess {Number}  data.pageSize              每页展示数
     * @apiSuccess {Number}  data.total                 总数
     * @apiSuccess {Array}   data.list                  列表数据
     * @apiSuccess {Number}  data.list.id               用户ID
     * @apiSuccess {String}  data.list.department_name  部门名称
     * @apiSuccess {String}  data.list.member_num       部门人数
     * @apiSuccess {String}  data.list.sort             排序
     * @apiSuccess {String}  data.list.status_str       状态
     * @apiSuccess {Object}  data.condition             筛选条件
     * @apiSuccess {Array}   data.condition.status      状态[{value:'0', label: '普通'}]
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        $service = new DepartmentService();
        // 查询结果
        $data = $service->getListPage($params, (int)$page, (int)$pageSize);
        // 格式化数据
        $data['list'] = $service->getListPageFormat($data['list']);
        // 筛选项
        $data['condition'] = $service->getSelectCondition();
        return $data;
    }

    /**
     * @api {get} /v1/department/tree-list  部门列表【改版】
     * @apiName tree-list
     * @apiGroup Department
     * @apiUse CommonParam
     *
     *
     * @apiSuccess {Object}  data                       返回数据
     * @apiSuccess {Number}  data.page                  页码
     * @apiSuccess {Number}  data.pageSize              每页展示数
     * @apiSuccess {Number}  data.total                 总数
     * @apiSuccess {Array}   data.list                  列表数据
     * @apiSuccess {Number}  data.list.id               用户ID
     * @apiSuccess {String}  data.list.department_name  部门名称
     * @apiSuccess {String}  data.list.member_num       部门人数
     * @apiSuccess {String}  data.list.sort             排序
     * @apiSuccess {String}  data.list.status_str       状态
     * @apiSuccess {Object}  data.condition             筛选条件
     * @apiSuccess {Array}   data.condition.status      状态[{value:'0', label: '普通'}]
     */
    public function actionTreeList()
    {
//        $params = Yii::$app->request->get();
        $service = new DepartmentService();
        // 格式化数据
        return $service->getTreeListFormat($service->getTreeList());
    }

    /**
     * @api {get} /v1/department/info 部门详情
     * @apiName info
     * @apiGroup Department
     * @apiUse CommonParam
     *
     * @apiParam {Number} id 部门ID【新增ID为父级，编辑当前ID】
     * @apiParam {String} scenario 场景【create新增、edit编辑】
     *
     * @apiSuccess {Object} data                    返回数据
     * @apiSuccess {Number} data.id                 ID
     * @apiSuccess {Number} data.department_name    部门名称
     * @apiSuccess {Number} data.status             状态
     * @apiSuccess {Number} data.sort               排序
     */
    public function actionInfo()
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;
        $scenario = $params['scenario'] ?? 'create';

        // 查询结果
        $service = new DepartmentService();
        return $service->getInfo((int)$id, $scenario);
    }

    /**
     * @api {get} /v1/department/delete-info 部门删除详情
     * @apiName delete-info
     * @apiGroup Department
     * @apiUse CommonParam
     *
     * @apiParam {Number} id 部门ID
     *
     * @apiSuccess {Object} data                    返回数据
     * @apiSuccess {Number} data.has_child          是否存在子部门【1存在、0不存在】
     * @apiSuccess {Number} data.member_cnt         部门人数
     */
    public function actionDeleteInfo()
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;

        // 查询结果
        $service = new DepartmentService();
        return $service->getDeleteInfo((int)$id);
    }

    /**
     * @api {post} /v1/department/create 新增部门
     * @apiName create
     * @apiGroup Department
     * @apiUse CommonParam
     *
     * @apiBody {Number} pid                部门ID
     * @apiBody {String} department_name    部门名称
     * @apiBody {Number} data_level         数据权限
     * @apiBody {Array}  select_role_ids    角色IDs
     * @apiBody {Number} default_role_id    默认角色ID
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         部门ID
     */
    public function actionCreate()
    {
        $params = Yii::$app->request->post();
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new DepartmentForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $service = new DepartmentService();
        return $service->create($model);
    }

    /**
     * @api {post} /v1/department/update 编辑部门
     * @apiName update
     * @apiGroup Department
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                 部门ID
     * @apiBody {String} department_name    部门名称
     * @apiBody {Number} data_level         数据权限
     * @apiBody {Array}  select_role_ids    角色IDs
     * @apiBody {Number} default_role_id    默认角色ID
     * @apiBody {Array}  leader_member_ids  部门负责人IDs
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         部门ID
     */
    public function actionUpdate()
    {
        $params = Yii::$app->request->post();
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new DepartmentForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $service = new DepartmentService();
        return $service->edit($model);
    }

    /**
     * @api {post} /v1/department/move 转移部门
     * @apiName move
     * @apiGroup Department
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                 部门ID
     * @apiBody {Number} pid                部门ID
     * @apiBody {Number} after_id           转移后下一个部门ID【最后则传0】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         部门ID
     */
    public function actionMove()
    {
        $params = Yii::$app->request->post();
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new DepartmentForm();
        $model->setScenario('move');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $service = new DepartmentService();
        return $service->move($model);
    }

    /**
     * @api {post} /v1/department/delete 删除部门
     * @apiName delete
     * @apiGroup Department
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                部门ID
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         部门ID
     */
    public function actionDelete()
    {
        $params = Yii::$app->request->post();

        $model = new DepartmentForm();
        $model->setScenario('delete');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $service = new DepartmentService();
        return $service->delete($model);
    }
}
